package com.wbdgj.ui.branch;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.internal.LinkedTreeMap;
import com.wbdgj.R;
import com.wbdgj.adapter.ChoiceBranchAdapter;
import com.wbdgj.adapter.ChoiceBranchOutAdapter;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.home.CampaignDetailActivity;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.utils.dialog.ConfirmDialog;
import com.wbdgj.utils.refresh.PullListView;
import com.wbdgj.utils.refresh.PullToRefreshLayout;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;

public class ChoiceBranchOutActivity extends BaseActivity {

    private Context context = this;
    private String branchId;

    @BindView(R.id.demo_listview)
    PullListView demo_listview;
    @BindView(R.id.pullToRefreshLayout)
    PullToRefreshLayout myPullToRefreshLayout;
    ChoiceBranchOutAdapter choiceBranchAdapter;

    @BindView(R.id.text_top_right)
    TextView text_top_right;

    @BindView(R.id.search_edit)
    EditText editSearch;

    private String KEYWORD;

    @Override
    protected int getLayoutResId() {
        return R.layout.ui_choice_branch;
    }

    @Override
    protected void initView() {
        text_top_right.setText("请选择转出门店");
        branchId = getIntent().getStringExtra("id");
        rocordgroupa();

        editSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_SEND ||( keyEvent != null && keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    KEYWORD = editSearch.getText().toString();
                    rocordgroupa();
                    return true;
                }
                return false;
            }
        });

        demo_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                if (!choiceBranchAdapter.hasNoData){
//                    if (decimalFormat.format(choiceBranchAdapter.getItem(i).get("union_type")).equals("9")){
//                        final ConfirmDialog confirmDialog = new ConfirmDialog(context, "当前门店未加入漫游联盟，暂不能进行漫游！" , 8);
//                        confirmDialog.show();
//                        confirmDialog.setCanceledOnTouchOutside(false);
//                        confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
//                            @Override
//                            public void doConfirm() {
//                                confirmDialog.dismiss();
//                            }
//
//                            @Override
//                            public void doCancel() {
//                                confirmDialog.dismiss();
//                            }
//                        });
//                    }else{
//                        Intent intent = new Intent();
//                        intent.putExtra("union_type", decimalFormat.format(choiceBranchAdapter.getItem(i).get("union_type"))+"");
//                        intent.putExtra("branch_id", decimalFormat.format(choiceBranchAdapter.getItem(i).get("branch_id"))+"");
//                        intent.putExtra("branch_name", choiceBranchAdapter.getItem(i).get("branch_name")+"");
//                        setResult(3, intent);
//                        finish();
//                    }

                    Intent intent = new Intent();
                    intent.putExtra("union_type", decimalFormat.format(choiceBranchAdapter.getItem(i).get("union_type"))+"");
                    intent.putExtra("branch_id", decimalFormat.format(choiceBranchAdapter.getItem(i).get("branch_id"))+"");
                    intent.putExtra("branch_name", choiceBranchAdapter.getItem(i).get("branch_name")+"");
                    setResult(3, intent);
                    finish();
                }
            }
        });

        myPullToRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        rocordgroupa();
                        myPullToRefreshLayout.refreshFinish(true);
                        ToastUtils.toastShort("刷新成功");
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                myPullToRefreshLayout.loadMoreFinish(true);
            }
        });
    }

    private void rocordgroupa() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().rocordgroupa(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN), branchId, KEYWORD);
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    LinkedTreeMap linkedTreeBranch = (LinkedTreeMap) linkedTreeMap.get("branch");
                    List<LinkedTreeMap> linkedTreeMaps  = (List<LinkedTreeMap>) linkedTreeBranch.get("data");
                    choiceBranchAdapter = new ChoiceBranchOutAdapter(context, linkedTreeMaps);
                    demo_listview.setAdapter(choiceBranchAdapter);
                }
            }
        });
    }
}
